package egovframework.npqs.pqi.mobile.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import sun.misc.BASE64Decoder;

import com.google.gson.Gson;

//import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;

import egovframework.npqs.pqi.mobile.common.ByteInputStream;

/*
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
*/
import egovframework.npqs.pqi.mobile.service.MobileService;


@Service("mobileService")
public class MobileServiceImpl implements MobileService {

	@Resource(name = "mobileDAO")
	private MobileDAO mobileDAO;
	
	@Autowired
	private ServletContext servletContext;
	
//	private String filePath =  rootRealPath+"upload"+delimeter+"fe"+delimeter+"pqi"+delimeter+"mobile"+delimeter+yyyymmdd+delimeter;
	
	private String filePath ="";


	public List<Map<String, String>> selectFirstQuery(@RequestParam Map param) {
		// TODO Auto-generated method stub
		
		Map<String, String> reqstListMap = new HashMap<String, String>();

		
		List<Map<String, String>> dataList = null;
		try {
			dataList = mobileDAO.jsonReceiverList(param);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return dataList;
	}

	public List<Map<String, String>> selectReq002Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
	
		List<Map<String, String>> dataList = mobileDAO.selectReq002Query(paramMap);
		
		
		return dataList;
		
	}
	
	public List<Map<String, String>> selectReq004Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
	
		List<Map<String, String>> dataList = mobileDAO.selectReq004Query(paramMap);
		
		
		return dataList;
		
	}
	
	
	public List<Map<String, String>> selectReq004DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
	
		List<Map<String, String>> dataList = mobileDAO.selectReq004DetailQuery(paramMap);
		
		
		return dataList;
		
	}


	public List<Map<String, String>> loginSessionContainer(Map paramMap) throws Exception {

		List<Map<String, String>> dataList = mobileDAO.loginSessionContainer(paramMap);
		
		return dataList;
	
	}

	public void insertReq005Query(List<Map> paramMap) throws Exception {
		// TODO Auto-generated method stub

		mobileDAO.insertReq005Query(paramMap);
		
	}
	
	public void insertReq005Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		String resultData="";
		resultData = request.getParameter("resultData");
		
		JSONObject jsonParam = JSONObject.fromObject(resultData);

		JSONArray jsonArray = jsonParam.getJSONArray("resultData");
		
		List<Map> mList = new ArrayList<Map>();
		
		String offCd =""; /*검역신청지코드*/
		String recNo =""; /*검역신청번호*/
		String recSeq =""; /*검역신청순번*/
		String itmQty =""; /**/
		String qunCd =""; /**/
		String updNamCd="";
		String prdlstCode =""; /**/
		String hsCd =""; /**/
		String prdlstNm="";
		
		Map<String,Object> insertMap = null;

		for (Iterator<?> i = jsonArray.iterator(); i.hasNext();) {

			JSONObject ob = (JSONObject) i.next();
			
			insertMap = new HashMap<String,Object>();

			offCd = ob.getString("offCd");
			recNo = ob.getString("recNo");
			recSeq = ob.getString("recSeq");
			itmQty = ob.getString("itmQty");
			qunCd = ob.getString("qunCd");
			prdlstCode = ob.getString("prdlstCode");
			hsCd = ob.getString("hsCd");
			prdlstNm = ob.getString("prdlstNm");
			updNamCd = ob.getString("updNamCd")==null? "2": ob.getString("updNamCd");
			
			insertMap.put("offCd", offCd);
			insertMap.put("recNo", recNo);
			insertMap.put("recSeq", recSeq);
			insertMap.put("itmQty", itmQty);
			insertMap.put("qunCd", qunCd);
			insertMap.put("prdlstCode", prdlstCode);
			insertMap.put("hsCd", hsCd);
			insertMap.put("prdlstNm", prdlstNm);
			insertMap.put("updNamCd", updNamCd);
			
			mList.add(insertMap);

		}


		mobileDAO.insertReq005Query(mList);
		
	}
	
	//서버측 : String -> byte[]
	public byte[] castStringToByteArray(String str){
		String[] byteValues = str.substring(0, str.length()).split(",");
		byte[] bytes = new byte[byteValues.length];
		for (int i=0, len=bytes.length; i<len; i++) {
			   bytes[i] = Byte.valueOf(byteValues[i].trim());     
			}
		return bytes;
	}
	

	public void insertReq009Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		String resultMaster="";
		resultMaster = request.getParameter("resultMaster");
		JSONObject jo = JSONObject.fromObject(resultMaster);
		JSONObject jsonParam =  jo.getJSONObject("resultMaster");
		
		String resultDetail = ""; /*검역결과상세*/
		resultDetail = request.getParameter("resultDetail");
		
		JSONObject jsonParamDetail = JSONObject.fromObject(resultDetail);
		JSONArray jsonArrayDetail = jsonParamDetail.getJSONArray("resultDetail");  /*검역결과마스터*/
		List<Map<String,String>> dList = null;
		

		String resOffCd ="";/*검역결과검역지코드*/
		String resNo ="";	/*검역결과번호*/
		String msgId ="";	/*문서유통ID*/
		String regType=""; 	/*검역신청구분*/
		String offCd ="";	/*검역신청지코드*/
		String recNo ="";	/*검역신청번호*/
		String validflag =""; /*현장검역 or 전량하역*/
		String insPlace =""; /*검역장소*/
		String delaysayu="";/*합격보류사유여부*/
		String quranCd1=""; /*검역관1*/
		String quranCd2=""; /*검역관2*/
		String quranCd3=""; /*검역관1*/
		String quranCd4=""; /*검역관2*/
		String quranCd5=""; /*검역관1*/
		String quranCd6=""; /*검역관2*/
		String remark=""; /*검역종합의견*/
		String updNamCd=""; /*등록인*/
		String resultProgrsSt ="377002"; /*검역진행상태*/
		String fileId = ""; /* 파일Id*/
		
		String recSeq="";
		String insQty="";
		String qunCd="";
		String itemType="";
		
		Map<String,String> insertMasterMap = null;
	
		dList= this.getCheckedItemList(jsonArrayDetail);
		
		insertMasterMap = new HashMap<String,String>();
		
		resOffCd = (String)jsonParam.get("resOffCd") == null ?  "" : (String)jsonParam.getString("resOffCd"); /*검역결과검역지코드*/
		resNo = (String)jsonParam.getString("resNo") == null ?  "" : (String)jsonParam.getString("resNo"); /*검역결과번호*/
		regType = (String)jsonParam.getString("regType") == null ?  "" : (String)jsonParam.getString("regType"); /*검역신청구분*/
		offCd = (String)jsonParam.getString("offCd") == null ?  "" : (String)jsonParam.getString("offCd"); /*검역신청지코드*/
		recNo = (String)jsonParam.getString("recNo") == null ?  "" : (String)jsonParam.getString("recNo"); /*검역신청번호*/
		validflag = (String)jsonParam.getString("validflag") == null ?  "" : (String)jsonParam.getString("validflag"); /*현장검역 or 전량하역*/
		insPlace = (String)jsonParam.getString("fieArea") == null ?  "" : (String)jsonParam.getString("fieArea"); /*검역장소*/
		delaysayu = (String)jsonParam.getString("passResrveReasonCd") == null ?  "" : (String)jsonParam.getString("passResrveReasonCd"); /*합격보류사유여부*/
		quranCd1 = (String)jsonParam.getString("quranCd1") == null ?  "" : (String)jsonParam.getString("quranCd1"); /*검역관리스트*/
		quranCd2 = (String)jsonParam.getString("quranCd2") == null ?  "" : (String)jsonParam.getString("quranCd2"); /*검역관리스트*/
		//quranCd3 = (String)jsonParam.getString("quranCd3") == null ?  "" : (String)jsonParam.getString("quranCd3"); /*검역관리스트*/
		//quranCd4 = (String)jsonParam.getString("quranCd4") == null ?  "" : (String)jsonParam.getString("quranCd4"); /*검역관리스트*/
		//quranCd5 = (String)jsonParam.getString("quranCd5") == null ?  "" : (String)jsonParam.getString("quranCd5"); /*검역관리스트*/
		//quranCd6 = (String)jsonParam.getString("quranCd6") == null ?  "" : (String)jsonParam.getString("quranCd6"); /*검역관리스트*/
		remark = (String)jsonParam.getString("remark") == null ?  "" : (String)jsonParam.getString("remark"); /*검역종합의견*/
		updNamCd = (String)jsonParam.getString("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); /*검역종합의견*/
		fileId = (String)jsonParam.get("fileId") == null ?  "" : (String)jsonParam.getString("fileId"); /* 파일Id*/
		
		insertMasterMap.put("resOffCd", resOffCd);
		insertMasterMap.put("resNo", resNo);
		insertMasterMap.put("regType", regType);
		insertMasterMap.put("offCd", offCd);
		insertMasterMap.put("recNo", recNo);
		insertMasterMap.put("validflag", validflag);
		insertMasterMap.put("passResrveReasonCd", delaysayu); /*합격보류사유*/
		insertMasterMap.put("fieArea", insPlace); /*검역장소*/
		insertMasterMap.put("quranCd1", quranCd1);
		insertMasterMap.put("quranCd2", quranCd2);
		//insertMasterMap.put("quranCd3", quranCd3);
		//insertMasterMap.put("quranCd4", quranCd4);
		//insertMasterMap.put("quranCd5", quranCd5);
		//insertMasterMap.put("quranCd6", quranCd6);
		insertMasterMap.put("remark", remark);
		insertMasterMap.put("resultProgrsSt", resultProgrsSt);
		insertMasterMap.put("updNamCd", updNamCd);
		insertMasterMap.put("regNamCd", updNamCd);
		
		int endNo = 0; 
		String quaYearEndNo="";/*14000001, offcd=null*/
		String year =""; 
		
		if("".equals(resOffCd) && "".equals(resNo)){
			Map<String,String> newResNoMap =  new HashMap<String,String>();
		
			newResNoMap.put("searchOffCd", offCd);
			newResNoMap.put("searchNoCd", "322002");
			
			List<Map<String, String>> dataList = mobileDAO.selectEndNo(newResNoMap);
			
			if(null !=dataList){
				
				for(Map<String, String> tmap : dataList){
					
					endNo =  Integer.parseInt(String.valueOf(tmap.get("endNo")));
				
					quaYearEndNo = tmap.get("quaYearEndNo").toString();
					//System.out.print("endNo  :  "+endNo);
				}
				
				year = quaYearEndNo.substring(0,2);// 년도	
				resNo = year + lpad(String.valueOf(endNo)); /* 마지막 결과번호 */
				
			}
			insertMasterMap.put("resOffCd", offCd); /*검역결과검역지코드*/
			insertMasterMap.put("resNo", resNo); /*검역결과번호*/
			
			for( Map item : dList){
				
				item.put("resOffCd", offCd);
				item.put("resNo", resNo);
			}
			
			
		
		}else{
			
			insertMasterMap.put("resOffCd", offCd); /*검역결과검역지코드*/
			insertMasterMap.put("resNo", resNo); /*검역결과번호*/
			

			for( Map item : dList){
				
				item.put("resOffCd", offCd);
				item.put("resNo", resNo);
			}
			
		}
		
		boolean imgFlag = false;
		String imgResult = "";

		List<Map<String,String>> fileList = null;
		//if(imgFlag == true){
			fileList = markImgFile(request,insertMasterMap);
		//}
		
		mobileDAO.insertReq009Query(insertMasterMap,dList,endNo,fileList);
		
	}
	
	public void insertFileReq052Query(HttpServletRequest request) throws Exception {
		
		
		Date today = Calendar.getInstance().getTime();
		String delimeter = this.getDirectoryDelimeter();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy" + delimeter + "MM" + delimeter + "dd");
		String yyyymmdd = dateFormat.format(today);
		String serverRootRealPath = request.getSession().getServletContext().getRealPath("/");
		String rootRealPath = serverRootRealPath.replace("/", delimeter);
		
		filePath =  rootRealPath+"upload"+delimeter+"fe"+delimeter+"pqi"+delimeter+"mobile"+delimeter+yyyymmdd+delimeter;
		
		
	}
	
	/*public String setFile(String userID, String password, String attachID, String base64String) {

		  // TODO : step1. attachID 변수에서 메타 정보추출
		  class FileVO {
		    private String fileName;

		    public String getFileName() {
		      return fileName;
		    }

		    public void setFileName(String fileName) {
		      this.fileName = fileName;
		    }
		  }

		  Gson gson = new Gson();
		  FileVO fileVO = gson.fromJson(attachID, FileVO.class);

		  // TODO : step2. base64String 에서 파일 객체 추출
		  HashMap<String, File> map = new HashMap<String, File>();
		  
		

		  StringTokenizer st1 = new StringTokenizer(base64String, ";");
		  while(st1.hasMoreTokens()) {
		    String fileInfo = st1.nextToken();
		    StringTokenizer st2 = new StringTokenizer(fileInfo, ":");
		      while(st2.hasMoreTokens()) {
		        String fileName = st2.nextToken();
		        String fileBinary = st2.nextToken();

		        BASE64Decoder decoder = new BASE64Decoder();
		        try {
		          byte[] b = decoder.decodeBuffer(fileBinary);
		          File file = new File(filePath + fileName);
		          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
		          bos.write(b);

		          map.put(fileName, file);
		        } catch (IOException e) {
		          e.printStackTrace();
		        }
		     }
		  }

		  // TODO : step3. 추출된 정보를 바탕으로 기간계 시스템에 파일 저장

		  // TODO : step4. 프로세스 결과코드를 리턴
		  String errorCode = "G000";

		  return errorCode;
		}
	*/

	
	/*파일첨부*/
	public List<Map<String,String>> markImgFile(HttpServletRequest request,Map<String,String> masterMap) throws Exception{
		
		String resultMaster="";
		resultMaster = request.getParameter("resultMaster");
		JSONObject jo = JSONObject.fromObject(resultMaster);
		JSONObject jsonParam =  jo.getJSONObject("resultMaster");
		
		String resOffCd = (String)jsonParam.getString("resOffCd") == null ?  "" : (String)jsonParam.getString("resOffCd");
		String resNo = (String)jsonParam.getString("resNo") == null ?  "" : (String)jsonParam.getString("resNo");
		String fileId = (String)(jsonParam.get("fileId")) == null ? "" : (String)(jsonParam.get("fileId"));
		String updNamCd = (String)jsonParam.getString("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); 
		
		/*검역결과번호가 존재하지 않는 경우*/
		if("".equals(resNo)){
			
			resNo = masterMap.get("resNo"); 
			resOffCd =  masterMap.get("resOffCd");
			
		}
		
		boolean isFileId = false;
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("fileSe", "349401"); //현장모바일사진
		paramMap.put("resOffCd", resOffCd);
		paramMap.put("resNo", resNo);
		
		//파일아디가 없으면 신규로 만든다
		
		if( null == fileId || "".equals(fileId)){
			List<Map<String,String>> fileIdList = mobileDAO.selectFileMaxId(paramMap);
			if( null != fileIdList && fileIdList.size() > 0){
				fileId = String.valueOf(fileIdList.get(0).get("fileId")) ;
			}
			isFileId = true;
		}
		
		paramMap.put("fileId", fileId);
		
		//파일ID가 없으면 검역신청 테이블에 등록해준다
		//if(isFileId){
		//	mobileDAO.updateFileId(paramMap);
		//}
		
		//해당 검역신청번호의 사진만 삭제
		List<Map<String,String>> svrFileList = mobileDAO.selectFileList(paramMap);
		for( Map<String,String> file : svrFileList ){
			String svrFilePath = file.get("filePath");
			File svrFile = new File(svrFilePath);
			if( svrFile.exists()){
				svrFile.delete();
			}
		}
		mobileDAO.deleteFile(paramMap);
		
		List<byte[]> imgList = new ArrayList<byte[]>();
		for( int i=1 ; i<=4 ; i++){
			String imgResult = (String)(jsonParam.get("imgResult"+i));
			if( null != imgResult && !"".equals(imgResult)){
				byte[] imgByte = castStringToByteArray(imgResult);
				imgList.add(imgByte);
			}
		}
		
		Date today = Calendar.getInstance().getTime();
		String delimeter = this.getDirectoryDelimeter();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy" + delimeter + "MM" + delimeter + "dd");
		String yyyymmdd = dateFormat.format(today);
		String serverRootRealPath = request.getSession().getServletContext().getRealPath("/");
		String rootRealPath = serverRootRealPath.replace("/", delimeter);
		
		List<Map<String,String>> fileList = new ArrayList<Map<String,String>>();
		for(int i= 0 ; i<imgList.size() ; i++){
			byte[] imgByte = imgList.get(i);
			String fileName = resOffCd+resNo+"_0"+(i+1)+".jpg";
			String filePath =  rootRealPath+"upload"+delimeter+"fe"+delimeter+"pqi"+delimeter+"mobile"+delimeter+yyyymmdd+delimeter;
			File file = new File(filePath);
			if( !file.exists()){
				file.mkdirs();
			}
			
			String filePathName = filePath + fileName;
			
			System.out.println("filePathName :"+filePathName);
			
			file = new File(filePathName );
			
			Map<String,String> fileMap = new HashMap<String,String>();
			fileMap.put("fileSe", "349401"); //현장모바일사진
			fileMap.put("resOffCd", resOffCd);
			fileMap.put("resNo", resNo);
			fileMap.put("fileId", fileId);
			fileMap.put("updNamCd", updNamCd);
			fileMap.put("regNamCd", updNamCd);
			fileMap.put("fileName", fileName); 
			fileMap.put("filePath", filePathName);
			fileMap.put("fileSize", String.valueOf(file.length()));
			fileList.add(fileMap);
			
			int size = 0;
			ByteInputStream is =  new ByteInputStream(imgByte,0,imgByte.length);
			BufferedInputStream bis = new BufferedInputStream(is);
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			while( (size=bis.read()) != -1){
				bos.write(size);
			}
			bos.close();
			bis.close();
			
		}
		
		return fileList;
	}
	
	
	/*파일첨부(소독결과)*/
	public List<Map<String,String>> markDsnfImgFile(HttpServletRequest request,Map<String,String> masterMap) throws Exception{
		
		String resultMaster="";
		resultMaster = request.getParameter("resultMaster");
		JSONObject jo = JSONObject.fromObject(resultMaster);
		JSONObject jsonParam =  jo.getJSONObject("resultMaster");
		
		String dsnfResOffCd =""; /*소독결과지코드*/
		String dsnfResNo =""; /*소독결과번호*/
		String fileId =""; /*소독결과번호*/
		
		//String dsnfResNo = (String)jsonParam.getString("dsnfResNo") == null ?  "" : (String)jsonParam.getString("dsnfResNo");
		//String dsnfResOffCd = (String)jsonParam.getString("dsnfResOffCd") == null ?  "" : (String)jsonParam.getString("dsnfResOffCd");
		//String fileId = (String)(jsonParam.get("fileId")) == null ? "" : (String)(jsonParam.get("fileId"));
		String updNamCd = (String)jsonParam.getString("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); 
		
		/*검역결과번호가 존재하지 않는 경우*/
		if("".equals(dsnfResNo)){
			
			dsnfResOffCd = masterMap.get("dsnfResOffCd"); 
			dsnfResNo =  masterMap.get("dsnfResNo");
			
		}
		
		boolean isFileId = false;
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("fileSe", "349401"); //현장모바일사진
		paramMap.put("dsnfResOffCd", dsnfResOffCd); /*소독결과지코드*/
		paramMap.put("dsnfResNo", dsnfResNo); /*소독결과번호*/
		
		//파일아디가 없으면 신규로 만든다
		
		if( null == fileId || "".equals(fileId)){
			List<Map<String,String>> fileIdList = mobileDAO.selectDsnfFileMaxId(paramMap);
			if( null != fileIdList && fileIdList.size() > 0){
				fileId = String.valueOf(fileIdList.get(0).get("fileId")) ;
			}
			isFileId = true;
		}
		
		paramMap.put("fileId", fileId);
		
		//파일ID가 없으면 검역신청 테이블에 등록해준다
		//if(isFileId){
		//	mobileDAO.updateFileId(paramMap);
		//}
		
		//해당 검역신청번호의 사진만 삭제
		List<Map<String,String>> svrFileList = mobileDAO.selectFileList(paramMap);
		for( Map<String,String> file : svrFileList ){
			String svrFilePath = file.get("filePath");
			File svrFile = new File(svrFilePath);
			if( svrFile.exists()){
				svrFile.delete();
			}
		}
		mobileDAO.deleteFile(paramMap);
		
		List<byte[]> imgList = new ArrayList<byte[]>();
		for( int i=1 ; i<=4 ; i++){
			String imgResult = (String)(jsonParam.get("imgResult"+i));
			if( null != imgResult && !"".equals(imgResult)){
				byte[] imgByte = castStringToByteArray(imgResult);
				imgList.add(imgByte);
			}
		}
		
		Date today = Calendar.getInstance().getTime();
		String delimeter = this.getDirectoryDelimeter();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy" + delimeter + "MM" + delimeter + "dd");
		String yyyymmdd = dateFormat.format(today);
		String serverRootRealPath = request.getSession().getServletContext().getRealPath("/");
		String rootRealPath = serverRootRealPath.replace("/", delimeter);
		
		List<Map<String,String>> fileList = new ArrayList<Map<String,String>>();
		String fileName ="";
		for(int i= 0 ; i<imgList.size() ; i++){
			byte[] imgByte = imgList.get(i);
			fileName = dsnfResOffCd+dsnfResNo+"_0"+(i+1)+".jpg";
			String filePath =  rootRealPath+"upload"+delimeter+"fe"+delimeter+"pqi"+delimeter+"mobile"+delimeter+yyyymmdd+delimeter;
			File file = new File(filePath);
			if( !file.exists()){
				file.mkdirs();
			}
			
			String filePathName = filePath + fileName;
			
			System.out.println("filePathName :"+filePathName);
			
			file = new File(filePathName );
			
			Map<String,String> fileMap = new HashMap<String,String>();
			fileMap.put("fileSe", "349401"); //현장모바일사진
			fileMap.put("dsnfResOffCd", dsnfResOffCd); /*소독결과지코드*/
			fileMap.put("dsnfResNo", dsnfResNo); /*소독결과번호*/
			fileMap.put("fileId", fileId);
			fileMap.put("updNamCd", updNamCd);
			fileMap.put("regNamCd", updNamCd);
			fileMap.put("fileName", fileName); 
			fileMap.put("filePath", filePathName);
			fileMap.put("fileSize", String.valueOf(file.length()));
			fileList.add(fileMap);
			
			int size = 0;
			ByteInputStream is =  new ByteInputStream(imgByte,0,imgByte.length);
			BufferedInputStream bis = new BufferedInputStream(is);
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			while( (size=bis.read()) != -1){
				bos.write(size);
			}
			bos.close();
			bis.close();
			
		}
		
		return fileList;
	}
	
	public void insertReq047Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		String resultMaster="";
		resultMaster = request.getParameter("resultMaster");
		JSONObject jo = JSONObject.fromObject(resultMaster);
		JSONObject jsonParam =  jo.getJSONObject("resultMaster");
		
		String resultDetail = ""; /*검역결과상세*/
		resultDetail = request.getParameter("resultDetail");
		JSONObject jsonParamDetail = JSONObject.fromObject(resultDetail);
		JSONArray jsonArrayDetail = jsonParamDetail.getJSONArray("resultDetail");  /*검역결과마스터*/
		List<Map<String,String>> dList = null;
		

		String resOffCd ="";/*검역결과검역지코드*/
		String resNo ="";	/*검역결과번호*/
		String msgId ="";	/*문서유통ID*/
		String regType=""; 	/*검역신청구분*/
		String offCd ="";	/*검역신청지코드*/
		String recNo ="";	/*검역신청번호*/
		String validflag ="338002"; /*현장검역 or 전량하역*/
		String insPlace =""; /*검역장소*/
		String delaysayu="";/*합격보류사유여부*/
		String quranCd1=""; /*검역관1*/
		String quranCd2=""; /*검역관2*/
		String quranCd3=""; /*검역관1*/
		String quranCd4=""; /*검역관2*/
		String quranCd5=""; /*검역관1*/
		String quranCd6=""; /*검역관2*/
		String remark=""; /*검역종합의견*/
		String updNamCd=""; /*등록인*/
		String resultProgrsSt ="377002"; /*검역진행상태*/
		
		String recSeq="";
		String insQty="";
		String qunCd="";
		String itemType="";
		String remCd="";
		String dMark ="";
		String resultFlag =""; /*검역결과*/
		String lmoResult =""; /*lmo검역결과*/
		
		Map<String,String> insertMasterMap = null;
	
		dList= this.getCheckedExpItemList(jsonArrayDetail);
		
		insertMasterMap = new HashMap<String,String>();
		
		resOffCd = (String)jsonParam.get("resOffCd") == null ?  "" : (String)jsonParam.getString("resOffCd"); /*검역결과검역지코드*/
		resNo = (String)jsonParam.getString("resNo") == null ?  "" : (String)jsonParam.getString("resNo"); /*검역결과번호*/
		regType = (String)jsonParam.getString("regType") == null ?  "" : (String)jsonParam.getString("regType"); /*검역신청구분*/
		offCd = (String)jsonParam.getString("offCd") == null ?  "" : (String)jsonParam.getString("offCd"); /*검역신청지코드*/
		recNo = (String)jsonParam.getString("recNo") == null ?  "" : (String)jsonParam.getString("recNo"); /*검역신청번호*/
		//validflag = (String)jsonParam.getString("validflag") == null ?  "" : (String)jsonParam.getString("validflag"); /*현장검역 or 전량하역*/
		insPlace = (String)jsonParam.getString("fieArea") == null ?  "" : (String)jsonParam.getString("fieArea"); /*검역장소*/
		quranCd1 = (String)jsonParam.getString("quranCd1") == null ?  "" : (String)jsonParam.getString("quranCd1"); /*검역관리스트*/
		quranCd2 = (String)jsonParam.getString("quranCd2") == null ?  "" : (String)jsonParam.getString("quranCd2"); /*검역관리스트*/
		remCd = (String)jsonParam.getString("remCd") == null ?  "" : (String)jsonParam.getString("remCd"); /*부기사항코드*/
		dMark = (String)jsonParam.getString("dMark") == null ?  "" : (String)jsonParam.getString("dMark"); /*부기사항코드*/
		remark = (String)jsonParam.getString("remark") == null ?  "" : (String)jsonParam.getString("remark"); /*검역종합의견*/
		updNamCd = (String)jsonParam.getString("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); /*검역종합의견*/
		resultFlag = (String)jsonParam.getString("resultFlag") == null ?  "" : (String)jsonParam.getString("resultFlag"); /*검역결과*/
		
		
		insertMasterMap.put("resOffCd", resOffCd);
		insertMasterMap.put("resNo", resNo);
		insertMasterMap.put("regType", regType);
		insertMasterMap.put("offCd", offCd);
		insertMasterMap.put("recNo", recNo);
		insertMasterMap.put("validflag", validflag);
		insertMasterMap.put("passResrveReasonCd", delaysayu); /*합격보류사유*/
		insertMasterMap.put("fieArea", insPlace); /*검역장소*/
		insertMasterMap.put("quranCd1", quranCd1);
		insertMasterMap.put("quranCd2", quranCd2);
		//insertMasterMap.put("quranCd3", quranCd3);
		//insertMasterMap.put("quranCd4", quranCd4);
		//insertMasterMap.put("quranCd5", quranCd5);
		//insertMasterMap.put("quranCd6", quranCd6);
		insertMasterMap.put("remark", remark);
		insertMasterMap.put("remCd", remCd);
		insertMasterMap.put("dMark", dMark);
		insertMasterMap.put("resultProgrsSt", resultProgrsSt);
		insertMasterMap.put("updNamCd", updNamCd);
		insertMasterMap.put("regNamCd", updNamCd);
		
		int endNo = 0; 
		String quaYearEndNo="";/*14000001, offcd=null*/
		String year =""; 
		
		if("".equals(resOffCd) && "".equals(resNo)){
			Map<String,String> newResNoMap =  new HashMap<String,String>();
		
			newResNoMap.put("searchOffCd", offCd);
			newResNoMap.put("searchNoCd", "322002");
			
			List<Map<String, String>> dataList = mobileDAO.selectEndNo(newResNoMap);
			
			if(null !=dataList){
				
				for(Map<String, String> tmap : dataList){
					
					endNo =  Integer.parseInt(String.valueOf(tmap.get("endNo")));
				
					quaYearEndNo = tmap.get("quaYearEndNo").toString();
					System.out.print("endNo  :  "+endNo);
				}
				
				year = quaYearEndNo.substring(0,2);// 년도	
				resNo = year + lpad(String.valueOf(endNo)); /* 마지막 결과번호 */
				
			}
			insertMasterMap.put("resOffCd", offCd); /*검역결과검역지코드*/
			insertMasterMap.put("resNo", resNo); /*검역결과번호*/
			
			for( Map item : dList){
				
				item.put("resOffCd", offCd);/*검역결과검역지코드*/
				item.put("resNo", resNo);/*검역결과번호*/
				item.put("resultFlag", resultFlag);/*검역결과*/
			}
		
		}else{
			
			insertMasterMap.put("resOffCd", offCd); /*검역결과검역지코드*/
			insertMasterMap.put("resNo", resNo); /*검역결과번호*/
			

			for( Map item : dList){
				
				item.put("resOffCd", offCd);/*검역결과검역지코드*/
				item.put("resNo", resNo);/*검역결과번호*/
				item.put("resultFlag", resultFlag);/*검역결과*/
			}
			
		}
	
		List<Map<String,String>> fileList = null;
		//if(imgFlag == true){
			fileList = markImgFile(request,insertMasterMap);
		//}
		mobileDAO.insertReq047Query(insertMasterMap,dList,endNo,fileList);
		
	}
	
	
	public void insertReq037Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		
		String resultMaster="";
		resultMaster = request.getParameter("resultMaster");
		JSONObject jo = JSONObject.fromObject(resultMaster);
		JSONObject jsonParam =  jo.getJSONObject("resultMaster");
		
		String resultDetail = ""; /*검역결과상세*/
		resultDetail = request.getParameter("resultDetail");
		JSONObject jsonParamDetail = JSONObject.fromObject(resultDetail);
		JSONArray jsonArrayDetail = jsonParamDetail.getJSONArray("resultDetail");  /*검역결과마스터*/
		
		List<Map<String,String>> dList = null;
		
		dList= this.getReq037ItemList(jsonArrayDetail);
		
		 
		String validflag =""; /*검역구분코드*/
		String passResrveReasonCd =""; /*합격보류사유코드*/
		String remark =""; /*종합의견*/
		String quranCd1 =""; /*검역관코드 1*/
		String quranCd2 ="";  /*검역관코드 2*/
		String quranNamCd3 =""; /*검역관코드 1*/
		String quranNamCd4 ="";  /*검역관코드 2*/
		String quranNamCd5 =""; /*검역관코드 1*/
		String quranNamCd6 ="";  /*검역관코드 2*/
		String searchRecNoList ="";  /*검역신청번호리스트 (20-00000000,......)*/
		String resultProgrsSt ="377002"; /*진행상태코드*/
		String updNamCd =""; /*등록인*/
		String fieArea = ""; /*검역장소*/
		validflag = (String)jsonParam.get("validflag") == null ?  "" : (String)jsonParam.getString("validflag"); /*검역구분코드*/
		passResrveReasonCd = (String)jsonParam.get("passResrveReasonCd") == null ?  "" : (String)jsonParam.getString("passResrveReasonCd"); /*합격보류사유코드*/
		
		remark = (String)jsonParam.get("remark") == null ?  "" : (String)jsonParam.getString("remark"); /*종합의견*/
		quranCd1 = (String)jsonParam.get("quranCd1") == null ?  "" : (String)jsonParam.getString("quranCd1"); /*검역관코드1*/
		quranCd2 = (String)jsonParam.get("quranCd2") == null ?  "" : (String)jsonParam.getString("quranCd2"); /*검역관코드2*/
		//quranNamCd3 = (String)jsonParam.get("quranCd3") == null ?  "" : (String)jsonParam.getString("quranCd3"); /*검역관코드1*/
		//quranNamCd4 = (String)jsonParam.get("quranNamCd4") == null ?  "" : (String)jsonParam.getString("quranCd4"); /*검역관코드2*/
		//quranNamCd5 = (String)jsonParam.get("quranNamCd5") == null ?  "" : (String)jsonParam.getString("quranCd5"); /*검역관코드1*/
		//quranNamCd6 = (String)jsonParam.get("quranNamCd6") == null ?  "" : (String)jsonParam.getString("quranCd6"); /*검역관코드2*/
		searchRecNoList = (String)jsonParam.get("searchRecNoList") == null ?  "" : (String)jsonParam.getString("searchRecNoList"); /*검역신청번호리스트*/
		resultProgrsSt = (String)jsonParam.get("resultProgrsSt") == null ?  "" : (String)jsonParam.getString("resultProgrsSt"); /*진행상태코드*/
		updNamCd = (String)jsonParam.get("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); /*진행상태코드*/
		fieArea = (String)jsonParam.get("fieArea") == null ?  "" : (String)jsonParam.getString("fieArea"); /*검역장소*/
		
		String offCd="";
		String recNo="";
		
		int endNo = 0; 
		String quaYearEndNo="";/*14000001, offcd=null*/
		String year =""; 
		
		
		Map<String,String> resDetailMap = new HashMap<String,String>();   //검역결과번호
		
		
		List<Map<String,String>> mList = null;
		mList = new ArrayList<Map<String,String>>();
		
		Map<String,String> insertMasterMap = null;
		String searchArr[] = (searchRecNoList).split(",");
		/*검역신청번호*/
		if(null!=searchArr){
			
			/*검역신청번호 하나씩 뽑아오기*/
			for(int i=0; i<searchArr.length; i++){
				String offRecNo[] = searchArr[i].toString().split("-");
				
				insertMasterMap = new HashMap<String,String>();
				if(null !=offRecNo){
				  
				  offCd = offRecNo[0].toString();
				  recNo = offRecNo[1].toString();
				}
			
				insertMasterMap.put("offCd", offCd);
				insertMasterMap.put("recNo", recNo);
				insertMasterMap.put("validflag", validflag);
				insertMasterMap.put("passResrveReasonCd", passResrveReasonCd);
				
				//검역장소
				insertMasterMap.put("fieArea", fieArea);
				
				insertMasterMap.put("remark", remark);
				insertMasterMap.put("quranCd1", quranCd1);
				insertMasterMap.put("quranCd2", quranCd2);
				//insertMasterMap.put("quranNamCd3", quranNamCd3);
				//insertMasterMap.put("quranNamCd4", quranNamCd4);
				//insertMasterMap.put("quranNamCd5", quranNamCd5);
				//insertMasterMap.put("quranNamCd6", quranNamCd6);
				insertMasterMap.put("resultProgrsSt", resultProgrsSt);
				insertMasterMap.put("updNamCd", updNamCd);
				insertMasterMap.put("regNamCd", updNamCd);
				mList.add(insertMasterMap);	  
			}
		}
		
	
		String resNo ="";
		
		
		/*검역결과품목정보에서 신규가 있는냐??*/
		
		for( Map<String,String> item : dList ){
			
			String recOffNo =item.get("offCd") +  item.get("recNo");
			String resOffNo =item.get("resOffCd") +  item.get("resNo");
			
			Map<String,String> newResNoMap =  new HashMap<String,String>();
			newResNoMap.put("searchOffCd", offCd); /*검역지코드*/
			newResNoMap.put("searchNoCd", "322002"); 
			
			List<Map<String, String>> dataList = mobileDAO.selectEndNo(newResNoMap);
			
			
			
			if(null == resOffNo || "".equals(resOffNo)){
				
				for(Map<String, String> tmap : dataList){
					endNo =  Integer.parseInt(String.valueOf(tmap.get("endNo")));
					quaYearEndNo = tmap.get("quaYearEndNo").toString();
					year = quaYearEndNo.substring(0,2);// 년도
				}
				
				break;
			}
		}
		
		
		/*검역결과품목정보 저장*/
		for( Map<String,String> item : dList ){
			
			String recOffNo =item.get("offCd") +  item.get("recNo");
			String resOffNo =item.get("resOffCd") +  item.get("resNo");
			
			if(null == resOffNo || "".equals(resOffNo)){
				
				if( !resDetailMap.containsKey( recOffNo) ){
					resNo =year + lpad(String.valueOf(endNo));
					resDetailMap.put(recOffNo , item.get("offCd")+(resNo)); //검역결과번호
					endNo = endNo +1;
					
				}
				if(resDetailMap.containsKey(recOffNo)){
					item.put("resOffCd",this.substrOffNo(resDetailMap.get(recOffNo),"O"));
					item.put("resNo",this.substrOffNo(resDetailMap.get(recOffNo),"N"));
				}
			}else{
				
				for( Map<String,String> item3 : mList ){
					
					String tmpRecNo = item3.get("offCd") +  item3.get("recNo");
					
				
					if(tmpRecNo.equals(recOffNo) ){
						item3.put("resOffCd",item.get("resOffCd"));
						item3.put("resNo",item.get("resNo"));
					}
					
				}
				
			}
			
	    }// for end 검역결과 마스터 정보
		
		
		//검역결과 마스터 저장
		if( !resDetailMap.isEmpty()){
			for(Map.Entry<String, String> entry : resDetailMap.entrySet()){
				
				String Key = entry.getKey(); //key값
				String value = entry.getValue();//value 값
				
					for( Map<String,String> item3 : mList ){
					
						String tmpRecNo = item3.get("offCd") +  item3.get("recNo");
					
					if(tmpRecNo.equals(Key) ){
						item3.put("resOffCd",this.substrOffNo(value,"O"));
						item3.put("resNo",this.substrOffNo(value,"N"));
					}
				}
			}
		}
		
		mobileDAO.insertReq037Query(mList,dList,endNo);
		
		
	}
	
	
	public String substrOffNo( String offNo , String type){
		String returnValue = "";
		if( null != offNo && !"".equals(offNo) && offNo.length() == 10){
			if( "O".equals(type)){
				returnValue =  offNo.substring(0, 2);
			}else if( "N".equals(type)){
				returnValue =  offNo.substring(2, 10);
			}
		}else{
			returnValue =  offNo;
		}
		return returnValue;
	}
	
	
	/**
	 * @Method Name  : getCheckedItemList
	 * @작성일       : 2014. 1. 14. 
	 * @작성자       : ktm
	 * @변경이력     : 2014. 1. 14. - ktm 
	 * @Method 설명    : 품목을 리스트로 변경하여 리턴
	 * @param dsnfCmmndVO
	 * @return
	 */
	public List<Map<String,String>> getReq037ItemList(JSONArray jsonArray){
		
		List<Map<String,String>> dList = new ArrayList<Map<String,String>>();
		Map<String,String> insertDetailMap = null;
		
		String offCd =""; /*검역신청지코드*/
		String recNo =""; /*검역신청번호*/
		String recSeq =""; /*순번*/
		String resOffCd=""; /*결과검역지코드*/
		String resNo="";/*결과번호*/
		String resSeq = "";/*순번*/
		
		String qunCd = "";/*단위코드*/
		String insQty = "0";/*검역수량*/
		String updNamCd = ""; /*등록인*/
		
		String itemType =""; /*합격품코드*/
		String regType =""; /*신청방법코드*/
		String msgId =""; /*문서유통ID*/
		
		String pasQty ="0"; /*합격량*/
		String quaExtQty ="0"; /*검역제외수량*/
		String proExtQty ="0"; /*금지제외수량*/
		String antQty ="0";  /*소독수량*/
		String wasQty ="0"; /*폐기수량*/
		String isoGroQty ="0"; /*격리재배수량*/
		
		String aftDsuseTrgetCd ="339001"; /*사후검역여부 (해당없음)*/
		String dbyhsProcessCd ="334000"; /*병해충구분(해당없음)*/
		String resultFlag ="389003"; /*합격*/
		
		for (Iterator<?> i = jsonArray.iterator(); i.hasNext();) {

			JSONObject ob = (JSONObject) i.next();
			
			insertDetailMap = new HashMap<String,String>();

			offCd =(String)ob.getString("offCd")== null ?  "" : (String)ob.getString("offCd"); /*검역지코드*/
			recNo = (String)ob.getString("recNo")== null ?  "" : (String)ob.getString("recNo"); /*신청번호*/
			recSeq = (String)ob.getString("recSeq")== null ?  "" : (String)ob.getString("recSeq"); /*순번*/
			
			resOffCd =(String)ob.getString("resOffCd")== null ?  "" : (String)ob.getString("resOffCd"); /*검역지코드*/
			resNo = (String)ob.getString("resNo")== null ?  "" : (String)ob.getString("resNo"); /*신청번호*/
			resSeq = (String)ob.getString("recSeq")== null ?  "" : (String)ob.getString("recSeq"); /*순번*/
		
			qunCd = (String)ob.getString("qunCd")== null ?  "" : (String)ob.getString("qunCd"); /*수량단위*/
			insQty = (String)ob.getString("insQty")== null ?  "" : (String)ob.getString("insQty"); /*검역수량*/
			itemType = (String)ob.getString("itemType")== null ?  "" : (String)ob.getString("itemType"); /*합격품코드*/
			regType = (String)ob.getString("regType")== null ?  "" : (String)ob.getString("regType"); /*합격품코드*/
			msgId = (String)ob.getString("msgId")== null ?  "" : (String)ob.getString("msgId"); /*합격품코드*/
			updNamCd = (String)ob.getString("updNamCd") == null ?  "" : (String)ob.getString("updNamCd"); /*등록인*/
			//resultFlag = (String)ob.getString("updNamCd") == null ?  "" : (String)ob.getString("updNamCd"); /*등록인*/
			
			
			insertDetailMap.put("resOffCd", resOffCd); /*검역결과지코드*/
			insertDetailMap.put("resNo", resNo); /*검역결과번호*/
			insertDetailMap.put("resSeq", resSeq); /*순번*/
			
			insertDetailMap.put("offCd", offCd); /*검역신청지코드*/
			insertDetailMap.put("recNo", recNo); /*검역신청번호*/
			insertDetailMap.put("recSeq", recSeq); /*순번*/
			
			insertDetailMap.put("qunCd", qunCd); /*단위*/
			insertDetailMap.put("insQty", insQty);/*검역수량*/
			insertDetailMap.put("updNamCd", updNamCd);/*등록인*/
			insertDetailMap.put("regNamCd", updNamCd);/*등록인*/
			insertDetailMap.put("resultFlag", updNamCd);/*합격*/
			insertDetailMap.put("dbyhsProcessCd", dbyhsProcessCd);/*병해충구분(해당없음)*/
			insertDetailMap.put("aftDsuseTrgetCd", aftDsuseTrgetCd);/*사후검역여부 (해당없음)*/
			
			if("416001".equals(itemType))
			{
				insertDetailMap.put("pasQty", insQty); /*합격품*/
				
				pasQty = insQty;
			}else if("416002".equals(itemType)){ /*검역제외가공품*/
				quaExtQty = insQty;
			
			}else if("416003".equals(itemType)){ /*금지제외수량*/
				proExtQty = insQty;
			}else if("416004".equals(itemType)){/*격리재배량*/
				isoGroQty = insQty;
			}
			
			insertDetailMap.put("insQty", insQty);/*검역수량*/
			insertDetailMap.put("antQty", antQty);/*소독수량*/
			insertDetailMap.put("wasQty", wasQty);/*폐기수량*/
			
			insertDetailMap.put("isoGroQty", isoGroQty); /*격리재배수량*/
			insertDetailMap.put("pasQty", pasQty); /*합격품*/
			insertDetailMap.put("quaExtQty", quaExtQty); /*검역제외수량*/
			insertDetailMap.put("proExtQty", proExtQty); /*금지제외수량*/
			
			dList.add(insertDetailMap);

		}
	
		return dList;
	}
	
	/*소독결과등록*/
	public String insertReq015Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		String resultMaster="";
		resultMaster = request.getParameter("resultMaster");
		JSONObject jo = JSONObject.fromObject(resultMaster);
		JSONObject jsonParam =  jo.getJSONObject("resultMaster");
		
		String resultDetail = ""; /*검역결과상세*/
		resultDetail = request.getParameter("resultDetail");
		JSONObject jsonParamDetail = JSONObject.fromObject(resultDetail);
		JSONArray jsonArrayDetail = jsonParamDetail.getJSONArray("resultDetail");  /*검역결과마스터*/
		List<Map<String,String>> dList = null;
		
		
		String mdctMi =""; /*개방시작분*/
		String mdctHh =""; /*개방시작시*/
		String mdctDe =""; /*개방시작일자*/
		String antMetCd =""; /*소독방법*/
		String entrpsUnitSn =""; /*업체코드*/
		String rotDiv =""; /*순환장치*/
		String medQty ="";/*단위약량*/
		String openDe ="";/*개방종료일자*/
		String openHh ="";/*개방종료시*/
		String openMi ="";/*개방종료분*/
		String antMet ="";/*훈증방법*/
		String medNamCd1 =""; /*투약입회검역관1*/
		String medNamCd2 =""; /*투약입회검역관2*/
		String rooTemp =""; /*고내온도*/
		String dsnfInspctPlace =""; /*소독장소*/
		String opeNamCd1 ="";/*개방검역관1*/
		String opeNamCd2 =""; /*개방검역관2*/
		String medCd =""; /*약제코드*/
		String antMin ="";/*소독시간*/
		String fumQty =""; /*훈증용적*/
		String equKind =""; /*시설종류*/
		String updNamCd=""; /*등록인*/

		
		Map<String,String> insertMasterMap = null;
		Map<String,String> insertDetailMap = null;
		
		dList= this.insertReq015ItemList(jsonArrayDetail);
		
		insertMasterMap = new HashMap<String,String>();
		
		mdctMi = (String)jsonParam.get("mdctMi") == null ?  "" : (String)jsonParam.getString("mdctMi"); /*개방시작분*/
		mdctHh = (String)jsonParam.getString("mdctHh") == null ?  "" : (String)jsonParam.getString("mdctHh"); /*개방시작시*/
		mdctDe = (String)jsonParam.getString("mdctDe") == null ?  "" : (String)jsonParam.getString("mdctDe"); /*개방시작일자*/
		antMetCd = (String)jsonParam.getString("antMetCd") == null ?  "" : (String)jsonParam.getString("antMetCd"); /*소독방법*/
		entrpsUnitSn = (String)jsonParam.getString("entrpsUnitSn") == null ?  "" : (String)jsonParam.getString("entrpsUnitSn"); /*업체코드*/
		rotDiv = (String)jsonParam.getString("rotDiv") == null ?  "" : (String)jsonParam.getString("rotDiv"); /*순환장치*/
		medQty = (String)jsonParam.getString("medQty") == null ?  "" : (String)jsonParam.getString("medQty"); /*단위약량*/
		openDe = (String)jsonParam.getString("openDe") == null ?  "" : (String)jsonParam.getString("openDe"); /*개방종료일자*/
		openHh = (String)jsonParam.getString("openHh") == null ?  "" : (String)jsonParam.getString("openHh"); /*개방종료시*/
		openMi = (String)jsonParam.getString("openMi") == null ?  "" : (String)jsonParam.getString("openMi"); /*개방종료분*/
		antMet = (String)jsonParam.getString("antMet") == null ?  "" : (String)jsonParam.getString("antMet"); /*훈증방법*/
		medNamCd1 = (String)jsonParam.getString("medNamCd1") == "null" ?  "" : (String)jsonParam.getString("medNamCd1"); /*투약입회검역관1*/
		medNamCd2 = (String)jsonParam.getString("medNamCd2") == "null" ?  "" : (String)jsonParam.getString("medNamCd2"); /*투약입회검역관2*/
		rooTemp = (String)jsonParam.getString("rooTemp") == null ?  "" : (String)jsonParam.getString("rooTemp"); /*고내온도*/
		dsnfInspctPlace = (String)jsonParam.getString("dsnfInspctPlace") == null ?  "" : (String)jsonParam.getString("dsnfInspctPlace"); /*소독장소*/
		opeNamCd1 = (String)jsonParam.getString("opeNamCd1") == "null" ?  "" : (String)jsonParam.getString("opeNamCd1"); /*개방검역관1*/
		opeNamCd2 = (String)jsonParam.getString("opeNamCd2") == "null" ?  "" : (String)jsonParam.getString("opeNamCd2"); /*개방검역관2*/
		medCd = (String)jsonParam.getString("medCd") == null ?  "" : (String)jsonParam.getString("medCd"); /*약제코드*/
		antMin = (String)jsonParam.getString("antMin") == null ?  "" : (String)jsonParam.getString("antMin"); /*소독시간*/
		fumQty = (String)jsonParam.getString("fumQty") == null ?  "" : (String)jsonParam.getString("fumQty"); /*훈증용적*/
		equKind = (String)jsonParam.getString("equKind") == null ?  "" : (String)jsonParam.getString("equKind"); /*시설종류*/
		updNamCd = (String)jsonParam.getString("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); /*등록인*/

		
		insertMasterMap.put("offCd", "");/*검역신청지코드*/
		insertMasterMap.put("recNo", "");/*검역신청번호*/
		insertMasterMap.put("resOffCd", "");/*검역결과지코드*/
		insertMasterMap.put("resNo", "");/*결과번호*/
		insertMasterMap.put("cmmdOffCd", "");/*소독명령지코드*/
		insertMasterMap.put("cmmdNo", "");/*소독명령번호*/
		insertMasterMap.put("venCd", entrpsUnitSn);/*업체코드*/
		insertMasterMap.put("antArea", dsnfInspctPlace); /*소독장소*/
		insertMasterMap.put("antDatefr", mdctDe);/*개방시작일자*/
		insertMasterMap.put("antDatefrHh", mdctHh);/*개방시작시*/
		insertMasterMap.put("antDatefrMi", mdctMi);/*개방시작분*/
		insertMasterMap.put("antDateto", openDe); /*개방종료일자*/
		insertMasterMap.put("antDatetoHh", openHh);/*개방종료시*/
		insertMasterMap.put("antDatetoMi", openMi);/*개방종료분*/
		insertMasterMap.put("medCd", medCd);/*약제코드*/
		insertMasterMap.put("medNm", "");/*약제명*/
		insertMasterMap.put("medUnit", "");/*단위*/
		insertMasterMap.put("antMetCd", antMetCd);
		insertMasterMap.put("rotDiv", rotDiv);
		insertMasterMap.put("medQty", medQty); 
		insertMasterMap.put("antMet", antMet);
		insertMasterMap.put("medNamCd1", medNamCd1);
		insertMasterMap.put("medNamCd2", medNamCd2);
		insertMasterMap.put("rooTemp", rooTemp);
		insertMasterMap.put("opeNamCd1", opeNamCd1);
		insertMasterMap.put("opeNamCd2", opeNamCd2);
		insertMasterMap.put("antMin", antMin);
		insertMasterMap.put("fumQty", fumQty); /*훈증용적*/
		insertMasterMap.put("equKind", equKind);/*시설종류*/
		insertMasterMap.put("updNamCd", updNamCd);/*등록인*/
		insertMasterMap.put("regNamCd", updNamCd);/*등록인*/
		
		/*검역결과번호가 없는 경우 검역결과번호 가져오기*/
		/*검역결과품목정보에서 신규가 있는냐??*/
		
		int endNo = 0; 
		
		String quaYearEndNo="";/*14000001, offcd=null*/
		String year =""; 
		String newDsnfResNo = "";
		
		for( Map item : dList ){
			
			String dsnfResNo = (String)item.get("dsnfResNo")==null ? "":(String)item.get("dsnfResNo");
			String dsnfResOffCd = (String)item.get("dsnfResOffCd")==null ? "":(String)item.get("dsnfResOffCd");
			String offCd = (String)item.get("offCd"); 
			
			if("".equals(dsnfResOffCd) && "".equals(dsnfResNo)){
				
				Map<String,String> newResNoMap =  new HashMap<String,String>();
				
				newResNoMap.put("searchOffCd", offCd);
				newResNoMap.put("searchNoCd", "322004");
				
				List<Map<String, String>> dataList = mobileDAO.selectEndNo(newResNoMap);
				
				if(null !=dataList){
					
					for(Map<String, String> tmap : dataList){
						
						endNo =  Integer.parseInt(String.valueOf(tmap.get("endNo")));
					
						quaYearEndNo = tmap.get("quaYearEndNo").toString();
						//System.out.print("endNo  :  "+endNo);
						
						endNo = endNo + 1;
					}
					
					year = quaYearEndNo.substring(0,2);// 년도	
					newDsnfResNo = year + lpad(String.valueOf(endNo)); /* 마지막 결과번호 */
					
				}
				insertMasterMap.put("dsnfResOffCd", offCd); /*검역결과검역지코드*/
				insertMasterMap.put("dsnfResNo", newDsnfResNo); /*검역결과번호*/
				
				break;
			}
				
		}
		
		
		int dCnt = 0;
		
		for( Map<String,String> item : dList ){
			
			if(dList!=null){
				dCnt = dList.size();
			}
			
			String offCd = (String)item.get("offCd")==null ? "":(String)item.get("offCd");/*검역신청지코드*/
			String recNo = (String)item.get("recNo")==null ? "":(String)item.get("recNo");/*검역신청번호*/
			String dsnfResNo = (String)item.get("dsnfResNo")==null ? "":(String)item.get("dsnfResNo");/*소독결과번호*/
			String dsnfResOffCd = (String)item.get("dsnfResOffCd")==null ? "":(String)item.get("dsnfResOffCd");/*소독결과지코드*/
			
			
			if("".equals(dsnfResOffCd) && "".equals(dsnfResNo)){
				
				item.put("dsnfResOffCd", offCd);
				item.put("dsnfResNo", newDsnfResNo);
			}else{
				item.put("dsnfResOffCd", offCd);
				item.put("dsnfResNo", dsnfResNo);
				insertMasterMap.put("dsnfResOffCd", offCd); /*검역결과검역지코드*/
				insertMasterMap.put("dsnfResNo", dsnfResNo); /*검역결과번호*/
				
				/*소독결과등록 시 소독건수가 1개 인겨우 소독마스터에서 검역결과번호 입력하기*/
				if(dCnt == 1){
					insertMasterMap.put("offCd", offCd); /*소독결과번호*/
					insertMasterMap.put("recNo", recNo); /*검역신청번호*/
				}
			}
			
			
		}
		
		/*파일첨부*/
		
		
		
		List<Map<String,String>> fileList = null;
		fileList = markDsnfImgFile(request,insertMasterMap);
		mobileDAO.insertReq015Query(insertMasterMap,dList, endNo,fileList);
		
		
		String returnDsnfResOffCd = "";
		String dsnfResNo = "";
		
		
		returnDsnfResOffCd = (String)insertMasterMap.get("dsnfResOffCd");
		dsnfResNo = (String)insertMasterMap.get("dsnfResNo");
		
		
		
		return returnDsnfResOffCd+"-"+dsnfResNo;
		
	}
	
	
	/**
	 * @Method Name  : getCheckedItemList
	 * @작성일       : 2014. 1. 14. 
	 * @작성자       : ktm
	 * @변경이력     : 2014. 1. 14. - ktm 
	 * @Method 설명    : 품목을 리스트로 변경하여 리턴
	 * @param dsnfCmmndVO
	 * @return
	 */
	public List<Map<String,String>> insertReq015ItemList(JSONArray jsonArray){
		
		List<Map<String,String>> dList = new ArrayList<Map<String,String>>();
		Map<String,String> insertDetailMap = null;
		
		String dsnfResOffCd =""; /*소독결과검역지코드*/
		String dsnfResNo =""; /*소독결과번호*/
		
		String resOffCd=""; /*결과검역지코드*/
		String resNo="";/*결과번호*/
		String offCd  = "";/*신청검역지코드*/
		String recNo  = "";/*검역신청번호*/
		String resSeq = "";/*순번*/
		String qunCd = "";
		String antQty = "0"; /*소독수량*/
		String insQty = "0";/*검역수량*/
		
		String updNamCd = ""; /*등록인*/
		
		String antMetCd ="";/*소독방법*/
 
		
		for (Iterator<?> i = jsonArray.iterator(); i.hasNext();) {

			JSONObject ob = (JSONObject) i.next();
			
			insertDetailMap = new HashMap<String,String>();
			
			dsnfResOffCd = (String)ob.getString("dsnfResOffCd")== null ?  "" : (String)ob.getString("dsnfResOffCd");
			dsnfResNo = (String)ob.getString("dsnfResNo")== null ?  "" : (String)ob.getString("dsnfResNo");
			offCd =(String)ob.getString("offCd")== null ?  "" : (String)ob.getString("offCd");
			recNo = (String)ob.getString("recNo")== null ?  "" : (String)ob.getString("recNo");
			resSeq = (String)ob.getString("resSeq")== null ?  "" : (String)ob.getString("resSeq");
			qunCd = (String)ob.getString("qunCd")== null ?  "" : (String)ob.getString("qunCd");
			antQty = (String)ob.getString("antQty")== null ?  "" : (String)ob.getString("antQty");
			insQty = (String)ob.getString("insQty")== null ?  "" : (String)ob.getString("insQty");
			updNamCd = (String)ob.getString("updNamCd") == null ?  "" : (String)ob.getString("updNamCd"); /*등록인*/
			
			antMetCd = (String)ob.getString("antMetCd") == null ?  "" : (String)ob.getString("antMetCd");; /*소독방법*/
			
			
			insertDetailMap.put("dsnfResOffCd", dsnfResOffCd); /*소독결과지코드*/
			insertDetailMap.put("dsnfResNo", dsnfResNo); /*소독결과번호*/
			insertDetailMap.put("resOffCd", ""); /*검역결과지코드*/
			insertDetailMap.put("resNo", ""); /*검역결과번호*/
			insertDetailMap.put("offCd", offCd); /*검역신청지코드*/
			insertDetailMap.put("recNo", recNo); /*검역신청번호*/
			insertDetailMap.put("resSeq", resSeq); /*순번*/
			insertDetailMap.put("dsnfResSeq", resSeq);
			insertDetailMap.put("qunCd", qunCd); /*단위*/
			insertDetailMap.put("antResQty", antQty);/*소독수량*/
			insertDetailMap.put("insQty", insQty);/*검역수량*/
			insertDetailMap.put("antMetCd", antMetCd);/*소독방법*/
			
			insertDetailMap.put("updNamCd", updNamCd);/*등록인*/
			insertDetailMap.put("regNamCd", updNamCd);/*등록인*/
			
			dList.add(insertDetailMap);

		}
	
		return dList;
	}
	
	public void insertReq035Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
				
		String resultData="";
		resultData = request.getParameter("resultData");
		
		JSONObject jo = JSONObject.fromObject(resultData);
		
		JSONObject jsonParam =  jo.getJSONObject("resultData");
		
		String ediYn ="";/*EDI전송여부*/
		String handleKwa ="";/*처리과*/
		String draftBody ="";/*기안문내용*/
		String updNamCd ="";/*등록인*/
		String draftRem ="";/*기안문내용*/
		String draftCd =""; /*기안문코드*/
		String fullResNo =""; /*검역결과번호*/
		String formNo =""; /*결재번호*/
		
		
		ediYn = (String)jsonParam.get("ediYn") == null ?  "" : (String)jsonParam.getString("ediYn"); /*EDI전송여부*/
		handleKwa = (String)jsonParam.get("handleKwa") == null ?  "" : (String)jsonParam.getString("handleKwa"); /*처리과*/
		draftBody = (String)jsonParam.get("draftBody") == null ?  "" : (String)jsonParam.getString("draftBody"); /*기안문내용*/
		updNamCd = (String)jsonParam.get("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); /*등록인*/
		draftRem = (String)jsonParam.get("draftRem") == null ?  "" : (String)jsonParam.getString("draftRem"); /*기안문내용*/
		draftCd = (String)jsonParam.get("draftCd") == null ?  "" : (String)jsonParam.getString("draftCd"); /*기안문코드*/
		fullResNo = (String)jsonParam.get("fullResNo") == null ?  "" : (String)jsonParam.getString("fullResNo"); /*검역결과번호*/
		formNo = (String)jsonParam.get("formNo") == null ?  "" : (String)jsonParam.getString("formNo"); /*검역결과번호*/
		
		String searchArr[] = fullResNo.split("-"); /*검역결과번호*/
		
		String offCd ="";
		String jobNo ="";
		String newEndNoInt="";
		String newDrafYmd ="";
		int newEndNo =0;
		
		if(null != searchArr){
			offCd = searchArr[0].toString(); /*검역지코드*/
			jobNo = searchArr[1].toString(); /*검역결과번호*/
		}

		Map<String,String> insertDrafMap = new HashMap<String,String>();
		
		insertDrafMap.put("offCd", offCd); /*검역지코드*/
		
		insertDrafMap.put("jobNo", jobNo); /*검역결과번호*/
		insertDrafMap.put("jobSe", "354201"); /*drafId*/
		insertDrafMap.put("drafCd", draftCd); /*기안문코드*/
		insertDrafMap.put("drafId", updNamCd); /*기안자*/
		insertDrafMap.put("drafBody", draftBody);
		insertDrafMap.put("draftRem", draftRem); /*기타사항*/
		insertDrafMap.put("handleKwa", handleKwa);
		insertDrafMap.put("ediYn", ediYn);
		insertDrafMap.put("formNo", formNo);  /*결재번호*/
		insertDrafMap.put("updNamCd", updNamCd);
		
		if(null == formNo || "".equals(formNo)){
		
			List<Map<String, String>> dataList = mobileDAO.selectSanctnMaxEndNo(offCd); /*결재번호*/
			
			if(null !=dataList){
				for(Map<String, String> tmap : dataList){
					newEndNoInt =  String.valueOf(tmap.get("newEndNoInt"));
					newDrafYmd = tmap.get("newDrafYmd").toString();
					newEndNo = Integer.parseInt(String.valueOf(tmap.get("newEndNo")));				
					formNo = offCd + newDrafYmd + "B01" + l4pad(String.valueOf(newEndNo));
					
				}
			}
			insertDrafMap.put("formNo", formNo);
			
		}else{
			insertDrafMap.put("formNo", formNo);
		}
		
		newEndNo = newEndNo + 1;
		
		mobileDAO.insertReq035Query(insertDrafMap,newEndNo);
		
	}
	
	
	public void insertReq038Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
				
		String resultData="";
		resultData = request.getParameter("resultData");
		
		JSONObject jo = JSONObject.fromObject(resultData);
		
		JSONObject jsonParam =  jo.getJSONObject("resultData");
		
		String ediYn ="";/*EDI전송여부*/
		String handleKwa ="";/*처리과*/
		String draftBody ="";/*기안문내용*/
		String updNamCd ="";/*등록인*/
		String draftRem ="";/*기안문내용*/
		String draftCd =""; /*기안문코드*/
		String fullResNo =""; /*검역결과번호*/
		String formNo =""; /*결재번호*/
		
		
		ediYn = (String)jsonParam.get("ediYn") == null ?  "" : (String)jsonParam.getString("ediYn"); /*EDI전송여부*/
		handleKwa = (String)jsonParam.get("handleKwa") == null ?  "" : (String)jsonParam.getString("handleKwa"); /*처리과*/
		draftBody = (String)jsonParam.get("draftBody") == null ?  "" : (String)jsonParam.getString("draftBody"); /*기안문내용*/
		updNamCd = (String)jsonParam.get("updNamCd") == null ?  "" : (String)jsonParam.getString("updNamCd"); /*등록인*/
		draftRem = (String)jsonParam.get("draftRem") == null ?  "" : (String)jsonParam.getString("draftRem"); /*기안문내용*/
		draftCd = (String)jsonParam.get("draftCd") == null ?  "" : (String)jsonParam.getString("draftCd"); /*기안문코드*/
		fullResNo = (String)jsonParam.get("fullResNo") == null ?  "" : (String)jsonParam.getString("fullResNo"); /*검역결과번호*/
		formNo = (String)jsonParam.get("formNo") == null ?  "" : (String)jsonParam.getString("formNo"); /*검역결과번호*/
		
		String searchArrList[] = fullResNo.split(","); /*검역결과번호*/
		
		String offCd ="";
		String jobNo ="";
		String newEndNoInt="";
		String newDrafYmd ="";
		int newEndNo =0;
		
		
		List<Map<String,String>> drafList = new ArrayList<Map<String,String>>();
		
		Map<String,String> insertDrafMap  = null;
		
		if(null == formNo || "".equals(formNo)){
			
			if(searchArrList !=null) {
				
				for(int i=0; i<searchArrList.length; i++){
						
					String searchArr[] = searchArrList[i].split("-");
					
					if(null != searchArr){
						offCd = searchArr[0].toString(); /*검역지코드*/
					}
				}
			}
			
			List<Map<String, String>> dataList = mobileDAO.selectSanctnMaxEndNo(offCd); /*결재번호*/
			
			if(null !=dataList){
				
				for(Map<String, String> tmap : dataList){
					newEndNoInt =  String.valueOf(tmap.get("newEndNoInt"));
					newDrafYmd = tmap.get("newDrafYmd").toString();
					newEndNo = Integer.parseInt(String.valueOf(tmap.get("newEndNo")));				
				}
			}
		}
		
		
		if(searchArrList !=null) {
			
			for(int i=0; i<searchArrList.length; i++){
					
				String searchArr[] = searchArrList[i].split("-");
				
				if(null != searchArr){
					offCd = searchArr[0].toString(); /*검역지코드*/
					jobNo = searchArr[1].toString(); /*검역결과번호*/
				}
		
				insertDrafMap = new HashMap<String,String>();
				
				insertDrafMap.put("offCd", offCd); /*검역지코드*/
				insertDrafMap.put("jobNo", jobNo); /*검역결과번호*/
				insertDrafMap.put("jobSe", "354201"); /*drafId*/
				insertDrafMap.put("drafCd", draftCd); /*기안문코드*/
				insertDrafMap.put("drafId", updNamCd); /*기안자*/
				insertDrafMap.put("drafBody", draftBody);
				insertDrafMap.put("draftRem", draftRem); /*기타사항*/
				insertDrafMap.put("handleKwa", handleKwa);
				insertDrafMap.put("ediYn", ediYn);
				insertDrafMap.put("updNamCd", updNamCd);
				
				newEndNo = newEndNo + 1;
			
				formNo = offCd + newDrafYmd + "B01" + l4pad(String.valueOf(newEndNo));	
				
				insertDrafMap.put("formNo", formNo);
				
				drafList.add(insertDrafMap);
				
				}
			
			}
		
		mobileDAO.insertReq038Query(drafList,newEndNo);
	
	}

	/**
	 * @Method Name  : getCheckedItemList
	 * @작성일       : 2014. 1. 14. 
	 * @작성자       : ktm
	 * @변경이력     : 2014. 1. 14. - ktm 
	 * @Method 설명    : 품목을 리스트로 변경하여 리턴
	 * @param dsnfCmmndVO
	 * @return
	 */
	public List<Map<String,String>> getCheckedItemList(JSONArray jsonArray){
		
		List<Map<String,String>> dList = new ArrayList<Map<String,String>>();
		Map<String,String> insertDetailMap = null;
		
		String resOffCd=""; /*결과검역지코드*/
		String resNo="";/*결과번호*/
		String offCd  = "";/*신청검역지코드*/
		String recNo  = "";/*검역신청번호*/
		String recSeq = "";/*순번*/
		String insQty = "0";
		String qunCd = "";
		String itemType = ""; /*선택품목코드*/
		String itmQty ="0";

		String antQty = "0"; //소독수량
		String wasQty = "0"; //폐기수량
		String isoGroQty = "0"; //소독수량
		String quaExtQty = "0"; //검역제외수량
		String proExtQty = "0"; //금지제외수량
		String updNamCd=""; /*등록인*/
		String aftDsuseTrgetCd ="339001"; /*사후검역여부 (해당없음)*/
		String dbyhsProcessCd ="334000"; /*병해충구분(해당없음)*/
		String resultFlag ="389003"; /*합격*/
		String pasQty  = "0";

		
		
		for (Iterator<?> i = jsonArray.iterator(); i.hasNext();) {

			JSONObject ob = (JSONObject) i.next();
			
			insertDetailMap = new HashMap<String,String>();
			
			offCd = ob.getString("offCd") == null ?  "" : ob.getString("offCd");
			recNo = ob.getString("recNo") == null ?  "" : ob.getString("recNo");
			recSeq = ob.getString("recSeq") == null ?  "" : ob.getString("recSeq");
			insQty = ob.getString("insQty") == null ?  "" : ob.getString("insQty");
			qunCd = ob.getString("qunCd") == null ?  "" : ob.getString("qunCd");
			itemType = ob.getString("itemType") == null ?  "" : ob.getString("itemType");
			itmQty = ob.getString("itmQty") == null ?  "0" : ob.getString("itmQty");
			
			updNamCd = ob.getString("updNamCd") == null ?  "" : ob.getString("updNamCd"); /*등록인*/
			
			
			
			if("416001".equals(itemType))
			{
				insertDetailMap.put("pasQty", insQty); /*합격품*/
				
				pasQty = insQty;
			}else if("416002".equals(itemType)){ /*검역제외가공품*/
				quaExtQty = insQty;
			
			}else if("416003".equals(itemType)){ /*금지제외수량*/
				proExtQty = insQty;
			}else if("416004".equals(itemType)){ /*격리재배수량*/
				isoGroQty = insQty;
			}
			
			insertDetailMap.put("offCd", offCd); /*신청검역지코드*/
			insertDetailMap.put("recNo", recNo); /*검역수량*/
			insertDetailMap.put("recSeq", recSeq); /*순번*/
			insertDetailMap.put("resSeq", recSeq); /*검역수량*/
			
			insertDetailMap.put("qunCd", qunCd); /*검역수량*/
			insertDetailMap.put("insQty", insQty);/*검역수량*/
			insertDetailMap.put("antQty", antQty);/*소독수량*/
			insertDetailMap.put("wasQty", wasQty);/*폐기수량*/
			
			insertDetailMap.put("isoGroQty", isoGroQty); /*격리재배수량*/
			insertDetailMap.put("pasQty", pasQty); /*합격품*/
			insertDetailMap.put("quaExtQty", quaExtQty); /*검역제외수량*/
			insertDetailMap.put("proExtQty", proExtQty); /*금지제외수량*/
			
			insertDetailMap.put("aftDsuseTrgetCd", aftDsuseTrgetCd); /*사후검역여부*/
			insertDetailMap.put("dbyhsProcessCd", dbyhsProcessCd); /*병해충구분*/
			insertDetailMap.put("resultFlag", resultFlag); /*검역결과*/
			insertDetailMap.put("updNamCd", updNamCd);
			insertDetailMap.put("regNamCd", updNamCd);
			
			dList.add(insertDetailMap);

		}
	
		return dList;
	}
	
	
	/**
	 * @Method Name  : getCheckedItemList
	 * @작성일       : 2014. 1. 14. 
	 * @작성자       : ktm
	 * @변경이력     : 2014. 1. 14. - ktm 
	 * @Method 설명    : 품목을 리스트로 변경하여 리턴
	 * @param dsnfCmmndVO
	 * @return
	 */
	public List<Map<String,String>> getCheckedExpItemList(JSONArray jsonArray){
		
		List<Map<String,String>> dList = new ArrayList<Map<String,String>>();
		Map<String,String> insertDetailMap = null;
		
		String resOffCd=""; /*결과검역지코드*/
		String resNo="";/*결과번호*/
		String offCd  = "";/*신청검역지코드*/
		String recNo  = "";/*검역신청번호*/
		String recSeq = "";/*순번*/
		String insQty = "0";
		String qunCd = "";
		String itemType = ""; /*선택품목코드*/

		String antQty = "0"; //소독수량
		String wasQty = "0"; //폐기수량
		String isoGroQty = "0"; //소독수량
		String quaExtQty = "0"; //검역제외수량
		String proExtQty = "0"; //금지제외수량
		String updNamCd=""; /*등록인*/
		String aftDsuseTrgetCd ="339001"; /*사후검역여부 (해당없음)*/
		String dbyhsProcessCd ="334000"; /*병해충구분(해당없음)*/
		String resultFlag =""; /*합격*/
		String pasQty  = "0";
		String lmoResult="";/*lmo검역결과*/
		
		
		
		for (Iterator<?> i = jsonArray.iterator(); i.hasNext();) {

			JSONObject ob = (JSONObject) i.next();
			
			insertDetailMap = new HashMap<String,String>();
			
			offCd = ob.getString("offCd") == null ?  "" : ob.getString("offCd");
			recNo = ob.getString("recNo") == null ?  "" : ob.getString("recNo");
			recSeq = ob.getString("recSeq") == null ?  "" : ob.getString("recSeq");
			insQty = ob.getString("insQty") == null ?  "" : ob.getString("insQty");
			qunCd = ob.getString("qunCd") == null ?  "" : ob.getString("qunCd");
			updNamCd = ob.getString("updNamCd") == null ?  "" : ob.getString("updNamCd"); /*등록인*/
			lmoResult = ob.getString("lmoResult") == null ?  "" : ob.getString("lmoResult"); /*검역결과*/
			
			insertDetailMap.put("pasQty", insQty); /*합격품*/
			insertDetailMap.put("offCd", offCd); /*신청검역지코드*/
			insertDetailMap.put("recNo", recNo); /*검역수량*/
			insertDetailMap.put("recSeq", recSeq); /*순번*/
			insertDetailMap.put("resSeq", recSeq); /*검역수량*/
			
			insertDetailMap.put("qunCd", qunCd); /*검역수량*/
			insertDetailMap.put("insQty", insQty);/*검역수량*/
			insertDetailMap.put("antQty", antQty);/*소독수량*/
			insertDetailMap.put("wasQty", wasQty);/*폐기수량*/
			
			insertDetailMap.put("isoGroQty", isoGroQty); /*격리재배수량*/
			insertDetailMap.put("pasQty", insQty); /*합격품*/
			insertDetailMap.put("quaExtQty", quaExtQty); /*검역제외수량*/
			insertDetailMap.put("proExtQty", proExtQty); /*금지제외수량*/
			
			insertDetailMap.put("aftDsuseTrgetCd", aftDsuseTrgetCd); /*사후검역여부*/
			insertDetailMap.put("dbyhsProcessCd", dbyhsProcessCd); /*병해충구분*/
			insertDetailMap.put("resultFlag", resultFlag); /*합격구분*/
			insertDetailMap.put("lmoResult", lmoResult); /*lmo검역결과*/
			insertDetailMap.put("updNamCd", updNamCd);
			insertDetailMap.put("regNamCd", updNamCd);
			
			dList.add(insertDetailMap);

		}
	
		return dList;
	}
	

	/*0 붙히기*/
	private String lpad(String str) {
		String tmp = "";
		if( null != str && !"".equals(str)){
			int size = 6 - str.length();
			while(  tmp.length() < size ){
				tmp += "0";
			}
			str = tmp +str;
		}
		return str; 
	}

	/*0 붙히기*/
	private String l4pad(String str) {
		String tmp = "";
		if( null != str && !"".equals(str)){
			int size = 4- str.length();
			while(  tmp.length() < size ){
				tmp += "0";
			}
			str = tmp +str;
		}
		return str; 
	}
	
	public List<Map<String, String>> selectCommonCode(Map paramMap) throws Exception {

		List<Map<String, String>> dataList = mobileDAO.selectCommonCode(paramMap);
		
		return dataList;
	
	}

	public List<Map<String, String>> selectReq006Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		
		    
			List<Map<String, String>> dataList = mobileDAO.selectReq006Query(paramMap);
			
			
			return dataList;
	}

	public List<Map<String, String>> selectReq010Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq010Query(paramMap);
		
		
		return dataList;
		
	}

	public List<Map<String, String>> selectReq010DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq010DetailQuery(paramMap);
		
		
		return dataList;
	}

	public JSONObject jsonReceiverList(Map<String, String> map) {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = null;
		try {
			dataList = mobileDAO.jsonReceiverList(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String tablename = map.get("tablename");

		JSONArray jsonArray = JSONArray.fromObject(dataList);

		jsonMap.put("table", tablename);

		jsonMap.put(tablename, jsonArray);

		JSONObject jsonObject = JSONObject.fromObject(jsonMap);

		return jsonObject;
	}

	
	public List<Map<String, String>> selectReq014Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq014Query(paramMap);	
		
		return dataList;
	}

	public List<Map<String, String>> selectEndNo(Map paramMap) throws Exception {
		
		List<Map<String, String>> dataList = mobileDAO.selectEndNo(paramMap);	
		
		return dataList;
	}

	public List<Map<String, String>> selectReq017Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq017Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq017DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq017DetailQuery(paramMap);		
		return dataList;
	}
	
	public List<Map<String, String>> selectReq040Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
				List<Map<String, String>> dataList = mobileDAO.selectReq040Query(paramMap);		
				return dataList;
	}

	public List<Map<String, String>> selectReq040DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
				List<Map<String, String>> dataList = mobileDAO.selectReq040DetailQuery(paramMap);		
				return dataList;
	}

	public List<Map<String, String>> selectReq041Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
				List<Map<String, String>> dataList = mobileDAO.selectReq041Query(paramMap);		
				return dataList;
	}

	public List<Map<String, String>> selectReq041DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
				List<Map<String, String>> dataList = mobileDAO.selectReq041DetailQuery(paramMap);		
				return dataList;
	}

	public List<Map<String, String>> selectReq022Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq022Query(paramMap);		
		return dataList;
	}
	
	public List<Map<String, String>> selectReq023Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq023Query(paramMap);		
		return dataList;
	}
	public List<Map<String, String>> selectReq024Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq024Query(paramMap);		
		return dataList;
	}
	public List<Map<String, String>> selectReq024DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq024DetailQuery(paramMap);		
		return dataList;
	}
	
	public List<Map<String, String>> selectReq011Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq011Query(paramMap);		
		return dataList;
	}
	
	public List<Map<String, String>> selectReq012Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq012Query(paramMap);		
		return dataList;
	}
	
	public List<Map<String, String>> selectReq039Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq039Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectSanctnList(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectSanctnList(paramMap);
		
		return dataList;
	}

	public List<Map<String, String>> selectReq025Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq025Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq026Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq026Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq027Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq027Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq013Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq013Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq029Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq029Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq029DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq029DetailQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq029QuranQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq029QuranQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq030Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq030Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq016Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq016Query(paramMap);		
		return dataList;
	}


	public List<Map<String, String>> selectReq032Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq032Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq019Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq019Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq034Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq034Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq034DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq034DetailQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020DetailQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020LabQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020LabQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020DbyhsQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020DbyhsQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020DsnfQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020DsnfQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020DsuseQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020DsuseQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq020ItemQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq020ItemQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq036Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		
		String searchArr[] = ((String)paramMap.get("searchRecNoList")).split(",");

		//searchVO.setSearchProdList(searchArr);
		
		Map searchRecNoMap = new HashMap<String, Object>();
		
		searchRecNoMap.put("searchRecNoArr", searchArr);
		
		
		List<Map<String, String>> dataList = mobileDAO.selectReq036Query(searchRecNoMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq036DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		String searchArr[] = ((String)paramMap.get("searchRecNoList")).split(",");

		//searchVO.setSearchProdList(searchArr);
		
		Map searchRecNoMap = new HashMap<String, Object>();
		
		String resultProgrsSt = (String)paramMap.get("resultProgrsSt").toString(); /*검역진행상태*/
		searchRecNoMap.put("searchRecNoArr", searchArr);
		searchRecNoMap.put("searchStatus", resultProgrsSt);
		
		List<Map<String, String>> dataList = mobileDAO.selectReq036DetailQuery(searchRecNoMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq036QuranQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		String searchArr[] = ((String)paramMap.get("searchRecNoList")).split(",");

		//searchVO.setSearchProdList(searchArr);
		
		Map searchRecNoMap = new HashMap<String, Object>();
		
		searchRecNoMap.put("searchRecNoArr", searchArr);
		List<Map<String, String>> dataList = mobileDAO.selectReq036QuranQuery(searchRecNoMap);		
		return dataList;
	}

	

	/*수출검역대기조회*/
	public List<Map<String, String>> selectReq042Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq042Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq044Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq044Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq045Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq045Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq043DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq043DetailQuery(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq046Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq046Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq048Query(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq048Query(paramMap);		
		return dataList;
	}

	public List<Map<String, String>> selectReq0048DetailQuery(Map paramMap) throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectReq0048DetailQuery(paramMap);		
		return dataList;
	}

	public void insertReq049Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		String resultData = ""; /*검역결과상세*/
		resultData = request.getParameter("resultData");
		JSONObject jsonParamDetail = JSONObject.fromObject(resultData);
		JSONArray jsonArrayDetail = jsonParamDetail.getJSONArray("resultData");  /*검역결과마스터*/
		
		List<Map<String,String>> dList = new ArrayList<Map<String,String>>();
		Map<String,String> insertDetailMap = null;
		
		
		String resOffCd=""; /*결과검역지코드*/
		String resNo="";/*결과번호*/
		String exportAreaCd  = "";/*신청검역지코드*/
		String appointYear  = "";/*검역신청번호*/
		String resSeq = "";/*순번*/
		String qunCd = "";
		String appointQty = "0";/*검역수량*/
		
		String updNamCd = ""; /*등록인*/

		
		for (Iterator<?> i = jsonArrayDetail.iterator(); i.hasNext();) {

			JSONObject ob = (JSONObject) i.next();
			
			insertDetailMap = new HashMap<String,String>();
			
			resOffCd =(String)ob.getString("resOffCd")== null ?  "" : (String)ob.getString("resOffCd");
			resNo = (String)ob.getString("resNo")== null ?  "" : (String)ob.getString("resNo");
			resSeq = (String)ob.getString("resSeq")== null ?  "" : (String)ob.getString("resSeq");
			exportAreaCd = (String)ob.getString("exportAreaCd")== null ?  "" : (String)ob.getString("exportAreaCd");
			appointYear = (String)ob.getString("appointYear")== null ?  "" : (String)ob.getString("appointYear");
			appointQty = (String)ob.getString("appointQty")== null ?  "" : (String)ob.getString("appointQty");
			updNamCd = (String)ob.getString("updNamCd") == null ?  "" : (String)ob.getString("updNamCd"); /*등록인*/
			
			insertDetailMap.put("resOffCd", resOffCd); /*검역결과지코드*/
			insertDetailMap.put("resNo", resNo); /*검역결과번호*/
			insertDetailMap.put("resSeq", resSeq); /*순번*/
			insertDetailMap.put("exportAreaCd", exportAreaCd); /*검역신청지코드*/
			insertDetailMap.put("appointYear", appointYear); /*검역신청번호*/
			insertDetailMap.put("appointQty", appointQty);
			insertDetailMap.put("regNamCd", updNamCd);/*등록인*/
			
			dList.add(insertDetailMap);

		}
		
		mobileDAO.insertReq049Query(dList);
	
	}

	public void deleteReq050Query(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		String resultData="";
		resultData = request.getParameter("resultData");
		
		JSONObject jo = JSONObject.fromObject(resultData);
		
		JSONObject jsonParam =  jo.getJSONObject("resultData");
		
		String resOffCd ="";
		String resNo ="";
		String resSeq ="";
		
		
		resOffCd = (String)jsonParam.get("resOffCd") == null ?  "" : (String)jsonParam.getString("resOffCd"); /*EDI전송여부*/
		resNo = (String)jsonParam.get("resNo") == null ?  "" : (String)jsonParam.getString("resNo"); /*처리과*/
		resSeq = (String)jsonParam.get("resSeq") == null ?  "" : (String)jsonParam.getString("resSeq"); /*기안문내용*/
		
		
		
		Map<String,String> deleteMap = new HashMap<String,String>();
		
		
		deleteMap.put("resOffCd", resOffCd); /*검역결과번호*/
		deleteMap.put("resNo", resNo); /*drafId*/
		deleteMap.put("resSeq", resSeq); /*기안문코드*/
		
		
		mobileDAO.deleteReq050Query(deleteMap);
	}
	
	public String getDirectoryDelimeter() {
		String delimeter;
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			delimeter = "\\";
		} else {
			delimeter = "/";
		}
		return delimeter;
	}

	public List<Map<String, String>> selectExportYearList() throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, String>> dataList = mobileDAO.selectExportYearList();		
		return dataList;
	}
	
	
	
	
}
